﻿@using System.Globalization
@using BookShop.DTO
@using BookShop.Library.Common
@using BookShop.Library.Constant
@using PagedList
@using PagedList.Mvc
@model List<Book>
@{
    ViewBag.Title = "Quản lý Sách";
    var mod = Session[SessionKey.Moderator] != null ? (Moderator)Session[SessionKey.Moderator] : null;
}

<div class="portlet box blue">
    <div class="portlet-title">
        <div class="caption">
            <i class="fa fa-book"></i>@ViewBag.Title
        </div>
    </div>
    <div class="portlet-body form">
        <form class="form-horizontal" method="POST" action="@Url.Action("List")">
            <div class="form-body">
                <h3 class="form-section">Tìm kiếm</h3>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">ID</label>
                            <div class="col-md-4">
                                <input type="text" class="form-control" value="@ViewBag.SearchId" name="bookId" id="bookId"/>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Tiêu đề sách</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" value="@ViewBag.SearchTitle" name="title" id="title"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Tác giả</label>
                            <div class="col-md-8">
                                <select class="form-control input-large bs-select" id="authorId" name="authorId" data-live-search="true"></select>
                                <span class="help-block" for="authorId"></span>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Nhà xuất bản</label>
                            <div class="col-md-8">
                                <select class="form-control input-large bs-select" id="publisherId" name="publisherId" data-live-search="true"></select>
                                <span class="help-block" for="publisherId"></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Thể loại</label>
                            <div class="col-md-8">
                                <select class="form-control input-large bs-select" id="categoryId" name="categoryId" data-live-search="true"></select>
                                <span class="help-block" for="categoryId"></span>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Giá từ</label>
                            <div class="col-md-8">
                                <div class="input-group input-small">
                                    <input type="text" class="form-control input-small" name="fromPrice" id="fromPrice" value="@ViewBag.SearchFromPrice">
                                    <span class="input-group-addon">đến </span>
                                    <input type="text" class="form-control input-small" name="toPrice" id="toPrice" value="@ViewBag.SearchToPrice">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Trạng thái</label>
                            <div class="col-md-4">
                                <select class="form-control" name="status" id="status">
                                    <option value="">All</option>
                                    <option value="@BookStatus.Active">Active</option>
                                    <option value="@BookStatus.Inactive">Inactive</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-actions right">
                @if (CommonFunction.CheckModeratorRole(mod, ModeratorRole.BookWrite))
                {
                    <a target="_blank" href="@Url.Action("Add")" class="btn green" style="margin-right: 10px;"><i class="fa fa-plus"></i>Thêm mới</a>
                }
                <input type="submit" class="btn blue" value="Tìm kiếm" />
            </div>
        </form>
        <table class="table table-striped table-bordered table-hover dataTable" id="studyschedule_table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Tiêu đề sách</th>
                    <th>Thể loại</th>
                    <th>Tác giả</th>
                    <th>Nhà xuất bản</th>
                    <th>Giá</th>
                    <th>Trạng thái</th>
                    <th>Thao tác</th>
                </tr>
            </thead>
            <tbody>
                @if (Model == null || Model.Count == 0)
                {
                    <tr>
                        <td colspan="8">No data to display</td>
                    </tr>
                }
                else
                {
                    foreach (var book in Model)
                    {
                    <tr>
                        <td>@book.Id</td>
                        <td>@book.Title</td>
                        <td>@book.CategoryName</td>
                        <td>@book.AuthorName</td>
                        <td>@book.PublisherName</td>
                        <td>@book.Price</td>
                        <td>
                            @switch (book.Status)
                            {
                                case BookStatus.Active:
                                @("Active")
                                    break;
                                case BookStatus.Inactive:
                                @("Inactive")
                                    break;
                            }
                        </td>
                        <td>
                            <div class="btn-group">
                                <button type="button" class="btn blue dropdown-toggle" data-toggle="dropdown"><i class="fa fa-wrench"></i></button>
                                <ul class="dropdown-menu pull-right" role="menu">
                                    @if (CommonFunction.CheckModeratorRole(mod, ModeratorRole.BookWrite))
                                    {
                                        <li>
                                            <a target="_blank" href="@Url.Action("Edit", new { bookId = book.Id })">Sửa</a>
                                        </li>
                                        <li>
                                            <a href="@Url.Action("Delete", new { bookId = book.Id })" onclick="return confirm('Bạn có muốn xóa sách này?')">Xóa</a>
                                        </li>
                                    }
                                    <li>
                                        <a target="_blank" href="@Url.Action("Detail", new { bookId = book.Id })">Chi tiết</a>
                                    </li>
                                </ul>
                            </div>
                        </td>
                    </tr>
                    }
                }
            </tbody>
        </table>
        <div class="records-count">@ViewBag.RecordsCount</div>
        @Html.PagedListPager((IPagedList)ViewBag.PageList, page => Url.Action("List",
       new
       {
           page,
           bookId = ViewBag.SearchId,
           title = ViewBag.SearchTitle,
           categoryId = ViewBag.SearchCategoryId,
           authorId = ViewBag.SearchAuthorId,
           publisherId = ViewBag.SearchPublisherId,
           fromPrice = ViewBag.SearchFromPrice,
           toPrice = ViewBag.SearchToPrice,
           status = ViewBag.SearchStatus,
       }), new PagedListRenderOptions
       {
           DisplayLinkToFirstPage = PagedListDisplayMode.Always,
           DisplayLinkToLastPage = PagedListDisplayMode.Always,
           DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded,
           DisplayLinkToNextPage = PagedListDisplayMode.IfNeeded,
           DisplayLinkToIndividualPages = true,
           DisplayPageCountAndCurrentLocation = false,
           MaximumPageNumbersToDisplay = 5,
           DisplayEllipsesWhenNotShowingAllPageNumbers = true,
           EllipsesFormat = "&#8230;",
           LinkToFirstPageFormat = "««",
           LinkToPreviousPageFormat = "«",
           LinkToIndividualPageFormat = "{0}",
           LinkToNextPageFormat = "»",
           LinkToLastPageFormat = "»»",
           PageCountAndCurrentLocationFormat = "Page {0} of {1}.",
           ItemSliceAndTotalFormat = "Showing items {0} through {1} of {2}.",
           FunctionToDisplayEachPageNumber = null,
           ClassToApplyToFirstListItemInPager = null,
           ClassToApplyToLastListItemInPager = null,
           ContainerDivClasses = new[] { "pagination-container" },
           UlElementClasses = new[] { "pagination" },
           LiElementClasses = Enumerable.Empty<string>()
       })
    </div>
</div>

@section css
{
    <link href="@Url.Content("~/Content/assets/global/plugins/bootstrap-select/bootstrap-select.min.css")" rel="stylesheet" type="text/css"/>
    <link type="text/css" rel="stylesheet" href="@Url.Content("~/Content/assets/global/plugins/bootstrap-toastr/toastr.min.css")"/>
    <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/assets/global/plugins/bootstrap-datepicker/css/datepicker3.css")"/>
    <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/assets/global/plugins/bootstrap-daterangepicker/daterangepicker-bs3.css")"/>
}

@section javascript
{
    <script src="@Url.Content("~/Content/assets/global/plugins/bootstrap-select/bootstrap-select.min.js")" type="text/javascript"></script>
    <script type="text/javascript" src="@Url.Content("~/Content/assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Content/assets/global/plugins/bootstrap-toastr/toastr.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Content/assets/global/plugins/bootstrap-daterangepicker/daterangepicker.js")"></script>
    @if (TempData[AlertMessage.TempDataKey] != null)
    {
        <script type="text/javascript">
            toastr['error']('@TempData[AlertMessage.TempDataKey].ToString()', 'Error');
            toastr.option = {
                "closeButton": true,
                "debug": false,
                "positionClass": "toast-top-right",
                "onclick": null,
                "showDuration": "1000",
                "hideDuration": "1000",
                "timeOut": "5000",
                "extendedTimeOut": "1000",
                "showEasing": "swing",
                "hideEasing": "linear",
                "showMethod": "fadeIn",
                "hideMethod": "fadeOut"
            }
        </script>
    }
    <script type="text/javascript">
        $(document).ready(function () {
            $('.bs-select').selectpicker({
            });
            $('.date-picker').datepicker({
                rtl: Metronic.isRTL(),
                autoclose: true
            });
            getCategories();
            getAuthors();
            getPublishers();
            $('#status').val('@ViewBag.SearchStatus');
        });
        function getCategories() {
            $("#categoryId").empty();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetAllCategory")',
                data: {},
                success: function (categories) {
                    $("#categoryId").append('<option value="">All</option>');
                    $.each(categories, function (i, category) {
                        $("#categoryId").append('<option value="' + category.Id + '">' + category.Name + '</option>');
                    });
                    $('#categoryId').selectpicker('refresh');
                    $('#categoryId').selectpicker('val', '@ViewBag.SearchCategoryId');
                },
                error: function () {
                }
            });
            return false;
        }
        function getAuthors() {
            $("#authorId").empty();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetAllAuthor")',
                data: {},
                success: function (authors) {
                    $("#authorId").append('<option value="">All</option>');
                    $.each(authors, function (i, author) {
                        $("#authorId").append('<option value="' + author.Id + '">' + author.Name + '</option>');
                    });
                    $('#authorId').selectpicker('refresh');
                    $('#authorId').selectpicker('val', '@ViewBag.SearchAuthorId');
                },
                error: function () {
                }
            });
            return false;
        }
        function getPublishers() {
            $("#publisherId").empty();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetAllPublisher")',
                data: {},
                success: function (publishers) {
                    $("#publisherId").append('<option value="">All</option>');
                    $.each(publishers, function (i, publisher) {
                        $("#publisherId").append('<option value="' + publisher.Id + '">' + publisher.Name + '</option>');
                    });
                    $('#publisherId').selectpicker('refresh');
                    $('#publisherId').selectpicker('val', '@ViewBag.SearchPublisherId');
                },
                error: function () {
                }
            });
            return false;
        }
    </script>
}